package com.android.justcook;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import camp.android.justcook.R;

public class RecipesListActivity extends BaseListActivity {

	public String[] recipes;
	protected int catId;
	protected boolean REC_FAV = true;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	 setContentView(R.layout.activity_recipes);
    	//db
  		String[] categories = MainActivity.CATEGORIES;
  		int[] images = MainActivity.CATEGORY_IMAGES;
  		String[][] meals = MainActivity.MEALS;
  		String[][] mealsPoints = MainActivity.MEALS_POINTS;
  		String[][] mealsDescriptions = MainActivity.MEALS_DESCRIPTIONS;
    // Get the message from the intent
    	
        Intent intent = getIntent();
        catId = intent.getIntExtra(MainActivity.EXTRA_MESSAGE, 0);
        
        getActionBar().setTitle(categories[catId]);
        
        //Set cover image and text
        ImageView coverImage = (ImageView)findViewById(R.id.cover_image);
        coverImage.setImageResource(images[catId]);
        TextView coverCategory = (TextView)findViewById(R.id.cover_category);
        coverCategory.setText(categories[catId]);
        
       
        ListAdapter recipesAdapter = new RecipesListAdapter(getLayoutInflater(), meals[catId], 
        		mealsPoints[catId], mealsDescriptions[catId]);
        getListView().setAdapter(recipesAdapter);
        
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        
        super.onCreate(savedInstanceState);
    }

   
    public final static String EXTRA_MESSAGE = "com.android.justcook.MESSAGE";
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	Intent intent = new Intent(this, ShowRecipeActivity.class);
    	intent.putExtra(EXTRA_MESSAGE, (int) id);
    	intent.putExtra("CATEGORY_ID", catId);
    	startActivity(intent);
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void onToggleStar(View view)
	{
		ImageButton fav = (ImageButton) findViewById(R.id.favorite);
		if(REC_FAV)
		{
			fav.setBackgroundResource(R.drawable.starfull);
			REC_FAV = false;
		}
		else
		{
			fav.setBackgroundResource(R.drawable.starempty);
			REC_FAV = true;
		}
	}

}
